package com.gopay.bis.manager.bank.ebank;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.citic.ebank.pay.CiticB2bMessage;
import com.gopay.common.domain.bank.citic.ebank.pay.Row;
import com.gopay.common.domain.bank.citic.ebank.pay.TranList;
import com.gopay.common.domain.bank.citic.ebank.query.req.CiticB2bStreamReq;
import com.gopay.common.domain.bank.citic.ebank.query.res.data.CiticB2bStreamDataRes;
import com.gopay.common.domain.bank.citic.ebank.verify.CiticB2bStreamVerify;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @CiticB2bManager.java
 * @author wanggang
 * @2013-12-12 下午1:48:47 www.gopay.com.cn Inc.All rights reserved.
 */
public class CiticB2bManager implements BankManagerRemoteService {

	@Resource(name = "citicConfig")
	private Map<String, String> citicConfig;
	
	@Resource(name = "citicB2bBankServiceClient")
	private BankService citicB2bBankServiceClient;
	
	private final static String XML_HEADER = "<?xml version=\"1.0\" encoding=\"GB2312\" ?>";

	@Override
	public Object sign(BaseMessage msg) throws GopayException {
		CiticB2bMessage message = (CiticB2bMessage) msg;
		List<Row> rows = new ArrayList<Row>();
		TranList tran = new TranList();
		Row row = new Row();
		message.setBankUrl(message.getPayUrl());
		message.setScbUrl(message.getPayRuturnUrl());
		message.setVersion(citicConfig.get("CITIC.B2B.XML.VERSION"));
		message.setBizCode(citicConfig.get("CITIC.B2B.XML.BIZCODE"));
		message.setMctNo(message.getMerID());
		message.setNtfType(citicConfig.get("CITIC.B2B.XML.NTFTYPE"));
		message.setRepType(citicConfig.get("CITIC.B2B.XML.RESTYPE"));
		message.setPcbUrl(message.getPayNotifyUrl());
		message.setTranPeiod(citicConfig.get("CITIC.B2B.XML.TRANPERIOD"));
		row.setMctjnlNo(message.getOrderId());
		row.setOrderNo(message.getOrderId());
		row.setOrderTime(message.getOrderDate());
		row.setOrderAmt(message.getAmount().toString());
		row.setBsnNo(citicConfig.get("CITIC.B2B.XML.BSNNO"));
		row.setPayeeAccNo(citicConfig.get("CITIC.B2B.XML.PAYEEACCNO"));
		row.setCryCode(citicConfig.get("CITIC.B2B.XML.CRYCODE"));
		row.setMcdName(citicConfig.get("CITIC.B2B.XML.MCDNAME"));
		row.setMcdType(citicConfig.get("CITIC.B2B.XML.MCDTYPE"));
		row.setOrderDesc(citicConfig.get("CITIC.B2B.XML.ORDERDESC"));
		row.setPurpose(citicConfig.get("CITIC.B2B.XML.PURPOSE"));
		row.setBerName(citicConfig.get("CITIC.B2B.XML.BERNAME"));
		row.setOrderSrc(citicConfig.get("CITIC.B2B.XML.ORDERSRC"));
		rows.add(row);
		tran.setName("TRANLIST");
		tran.setRows(rows);
		message.setTran(tran);
		message.setPlainMessage(buildPlain(message));
		BankLogUtils.printB2BManagerSignSendLog(message);
		String sign = (String) citicB2bBankServiceClient.sign(message);
		message.setSignMessage(sign);
		BankLogUtils.printB2BManagerSignReturnLog(message);
		return message;
	}

	@Override
	public Object verify(BaseMessage baseMessage) throws GopayException {
		CiticB2bMessage message = (CiticB2bMessage) baseMessage;
		CiticB2bStreamVerify stream = new CiticB2bStreamVerify();
		String plain = (String) citicB2bBankServiceClient.verify(message);
		XStream xs = new XStream(new DomDriver());
		xs.autodetectAnnotations(true);
		xs.toXML(stream);
		stream = (CiticB2bStreamVerify) xs.fromXML(plain, stream);
		return stream;
	}

	@Override
	public Object query(Object baseMessage) throws GopayException {
		CiticB2bMessage message = (CiticB2bMessage) baseMessage;
		CiticB2bStreamReq req = new CiticB2bStreamReq();
		req.setMctJnlNo(message.getOrderId());
		req.setMctNo(message.getMerID());
		req.setLocalTime(DateUtils.getCurrDate("yyyyMMddHHmmss"));
		String plain = buildPlain(req);
		message.setPlainMessage(plain);
		BankLogUtils.printB2BManagerQuerySendLog(message, req);
		CiticB2bStreamDataRes res = (CiticB2bStreamDataRes) citicB2bBankServiceClient.query(message);
		res.setOutStlmCd(BankConstants.BANK_LOGO_CITIC);
		BankLogUtils.printB2BManagerQueryReturnLog(res);
		return res;
	}

	/**
	 * 构建明文信息
	 * 
	 * @param message
	 * @return
	 */
	private String buildPlain(Object message) {
		XStream xs = new XStream(new DomDriver());
		xs.autodetectAnnotations(true);
		String plain = xs.toXML(message);
		StringBuffer xml = new StringBuffer(XML_HEADER);
		xml.append(plain);
		String fullXml = xml.toString();
		fullXml = fullXml.replaceAll("\n", "").replaceAll("\r", "");
		return fullXml;
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
